package com.example.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.result.Result;
import com.example.common.result.YiFeiException;
import com.example.entity.hz.Printtemplate;
import com.example.service.PrinttemplateService;
import com.example.mapper.PrinttemplateMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 14501
* @description 针对表【Printtemplate】的数据库操作Service实现
* @createDate 2024-09-03 11:16:28
*/
@Service
public class PrinttemplateServiceImpl extends ServiceImpl<PrinttemplateMapper, Printtemplate>
    implements PrinttemplateService{

    @Resource
    private PrinttemplateMapper printtemplateMapper;

    @Override
    public Result<List<Printtemplate>> queryPrintTemplateList(Integer uuid) {
        try {
            List<Printtemplate> printtemplateList = this.printtemplateMapper.selectList(new LambdaQueryWrapper<Printtemplate>().eq(Printtemplate::getUuid, uuid));
            return Result.success(printtemplateList);
        }catch (Exception e){
            throw new YiFeiException(500, "通过uuid查询打印模版发生异常"+e.getMessage());
        }
    }

    @Override
    @Transactional
    public Result savePrintTemplate(List<Printtemplate> printtemplateList) {
        try{
            if (!printtemplateList.isEmpty()){
                printtemplateList.forEach(arr -> {
                    arr.setCreationtime(DateUtil.format(new Date(), "yyyyMMdd"));
                    arr.setStatus("开启");
                    this.printtemplateMapper.insert(arr);
                });
            }
            return Result.success();
        }catch (Exception e){
            throw new YiFeiException(500,"新增打印模块时发生异常"+e.getMessage());
        }
    }

    @Override
    public Result deletePrintTemplate(List<Integer> uidList) {
        try {
            if (!uidList.isEmpty()){
                boolean b = this.removeByIds(uidList);
                if (b){
                    return Result.success();
                }
            }
            return Result.error("请输入正确参数");
        }catch (Exception e){
            throw new YiFeiException(500,"打印模版删除发生异常"+e.getMessage());
        }
    }

    @Override
    public Result<Printtemplate> queryOnePrintTemplate(Integer uid) {
        try {
            Printtemplate printtemplate = this.printtemplateMapper.selectById(uid);
            return Result.success(printtemplate);
        }catch (Exception e){
            throw new YiFeiException(500,"通过uid查询打印模版详细信息发生异常"+e.getMessage());
        }
    }

    @Override
    public Result updatePrintTemplate(Printtemplate printtemplate) {
        try {
            printtemplate.setUpdatetime(DateUtil.format(new Date(), "yyyyMMdd"));
            int update = this.printtemplateMapper.updateById(printtemplate);
            if (update>0){
                return Result.success();
            }
            return Result.error("修改失败!请检查传参{uid}");
        }catch (Exception e){
            throw new YiFeiException(500, "打印模版修改发生异常"+e.getMessage());
        }
    }
}




